<?php

/**
 * Validate class check data validation
 * @package Util
 * @author kovacsricsi + Sitemakers Kft
 */

class Validate
{
	/**
	 * Constructor protecte the classs, because it is stataic.
	 * @access private
	 * @throws Exception
	 * @return void
	 */
	private function __construct()
	{
		throw new Exception("Illegal operation, this class is only static class!");
	}

	/**
	 * Returns all variable from $_POST array, witch begin with "$prefix_".
	 * @access static
	 * @param string $prefix
	 * @return array
	 */
	public static function getFormData($prefix)
	{
		$ret = array();

		foreach ($_POST as $key => $field) {
			if (strpos($key, $prefix."_") !== false) {
				$newkey = str_replace($prefix."_", "", $key);
				$ret[$newkey] = trim($field);
			}
		}
		return $ret;
	}

	/**
	 * Validate date, valid format is yyyy-mm-dd.
	 * @access static
	 * @param string $date
	 * @return boolean
	 */
	public static function date($date)
	{
		$error = true;


		if (!preg_match("/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/i", $date)) {
			$error = false;
		}

		return $error;
	}

	/**
	 * Validate email.
	 * @access static
	 * @param string $email
	 * @return boolean
	 */
	public static function email($email)
	{
		$error = true;

		if (!preg_match("/^[a-z0-9]+([-_\.][a-z0-9]+)*@([a-z0-9]+([-.][a-z0-9]+)*)+\.[a-z]{2,}$/i", $email)) {
			$error = false;
		}

		return $error;
	}

	/**
	 * Validate passwd
	 * @access static
	 * @param string $pass
	 * @param string $pass2
	 * @return boolean
	 */
	public static function passwd($pass, $pass2)
	{
		$error = true;

		if ($pass != $pass2) {
			$error = false;
		}

		return $error;
	}

	/*
	 * Validate string as it contains not only space(s)
	 * @access static
	 * @param string $string
	 * @return boolean
	 */
	public static function isEmpty( $string, $charset = null ){

		if ($charset === null) {
			$charset = CHARSET;
		}

		$error = true;
		if ( mb_strlen( trim((string)$string), $charset) != 0){
			$error = false;
		}

		return $error;
	}


	/**
	 * Checks if a string contains characters [0-9A-Za-z_-]
	 * @access static
	 * @param  string
	 * @return boolean
	 */
	public static function isSafeString ($string)
	{
	    return (ereg("^[0-9A-Za-z_-]+$", $string)) ? true : false;
	}

}
?>
